package cn.lcc.utils.commom;

import cn.lcc.utils.utils.StrUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.util.Map;

/**
 * @author lccsetsun
 * @since 2024/1/31 10:46
 **/
public class MDCUtils {
	public MDCUtils() {
	}

	public static void set() {
		MDC.put(Constant.TRACE_ID, StrUtils.randomStringUpper(32));
	}

	public static void set(String requestId) {
		MDC.put(Constant.TRACE_ID, requestId);
	}

	public static String get() {
		String traceId = MDC.get(Constant.TRACE_ID);
		if (StringUtils.isBlank(traceId)){
			traceId = StrUtils.randomStringUpper(32);
		}
		return traceId;
	}

	public static void clear() {
		MDC.clear();
	}

	public static void setContextMap(Map<String, String> context) {
		MDC.setContextMap(context);
	}
}
